/* Includes ------------------------------------------------------------------*/
#include "MY_I2C_DRV.h"
#include "T117_MTS4_I2C.h"
/* Definition of GPIOI2C constants */
#define GPIOI2C_ACK  				((uint8_t)1)
#define GPIOI2C_NACK 				((uint8_t)0)
#define I2CWRITE_MASK  			((uint8_t)0xFE)
#define I2CREAD_MASK   			((uint8_t)0x01)


/* Exported functions ---------------------------------------------------------*/

void GPIOI2C_Bus_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_AHBPeriphClockCmd(I2C_SCL_BUSCLK, ENABLE);
	RCC_AHBPeriphClockCmd(I2C_SDA_BUSCLK, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	
	GPIO_Init(I2C_SCL_PORT, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = I2C_SDA_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_Init(I2C_SDA_PORT, &GPIO_InitStructure);
	
	i2c_set_sda();
	i2c_set_scl() ;
}

/* Support size=0 for dummy write */
/**-----------------------------------------------------------------------
  * @brief  T117发送指令函数
  * @param  DeviceAddr：地址  reg：寄存器地址  Data：写入数据
  * @retval 是否传输成功
-------------------------------------------------------------------------*/
uint8_t T117I2C_Transmit(uint8_t DeviceAddr, uint8_t reg, uint8_t Data)
{
	uint8_t ret=GPIOI2C_XFER_LASTACK;
	I2C_Start();
	if(I2C_master_write((DeviceAddr<<1) & I2CWRITE_MASK)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ADDRNACK;
	}

	if(I2C_master_write(reg)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;	
	}
	
	if(I2C_master_write(Data)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;	
	}	
	
	I2C_Stop();
	return ret;	
}

/* Application assure size>=1. */
/**-----------------------------------------------------------------------
  * @brief  T117数据接收函数
  * @param  DeviceAddr：地址  reg：寄存器地址  pData：读取数据
  * @retval 是否传输成功
-------------------------------------------------------------------------*/
uint8_t T117I2C_Receive(uint8_t DeviceAddr, uint8_t reg,uint8_t *pData)
{
	I2C_Start();
	if(I2C_master_write((DeviceAddr<<1) & I2CWRITE_MASK)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ADDRNACK;
	}
	
	if(I2C_master_write(reg)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;	
	}
	
	I2C_reStart();
	if(I2C_master_write((DeviceAddr<<1) | I2CREAD_MASK)== GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;
	}	
	
	(*pData)=I2C_master_read(GPIOI2C_NACK);
	
	I2C_Stop();

	return GPIOI2C_XFER_LASTNACK;
}

/*1MHzIIC*/
/**-----------------------------------------------------------------------
  * @brief  主机向芯片发送start指令
  * @param  无
  * @retval start标志位
-------------------------------------------------------------------------*/
void I2C_Start(void)
{
	i2c_set_sda();
	i2c_set_scl();
	MY_DELAY_US(1);
	i2c_clear_sda();
	MY_DELAY_US(1); 
	i2c_clear_scl();
}

/**-----------------------------------------------------------------------
  * @brief  主机向芯片发送restart指令
  * @param  无
  * @retval start标志位
-------------------------------------------------------------------------*/
void I2C_reStart(void)
{
	i2c_set_sda();
	MY_DELAY_US(1);  
	i2c_set_scl();
	MY_DELAY_US(1);  
	i2c_clear_sda();
	MY_DELAY_US(1); 
	i2c_clear_scl();
	MY_DELAY_US(1); 
}

/**-----------------------------------------------------------------------
  * @brief  主机向芯片发送stop指令
  * @param  无
  * @retval stop标志位
-------------------------------------------------------------------------*/
void I2C_Stop(void)
{  
	i2c_clear_sda();
	MY_DELAY_US(1);  
	i2c_set_scl();
	MY_DELAY_US(1);  
	i2c_set_sda();	
	MY_DELAY_US(1);
}

/**-----------------------------------------------------------------------
  * @brief  主机向芯片发送一个字节
  * @param  要发送的字节
  * @retval 是否应答
-------------------------------------------------------------------------*/
int I2C_master_write(uint8_t b)
{
  uint8_t mask = 0x80, ack;
	do
	{
		MY_DELAY_US(1);  /*tHD;DAT*/
		sda_wr_control(b);	
		MY_DELAY_US(1);  /*tSU_DAT*/	
		i2c_set_scl();
		MY_DELAY_US(1);  /*nHigh*/
		i2c_clear_scl();		
	}while ((mask>>=1) != 0);

	/* ACK slot checking */
	MY_DELAY_US(1);
	i2c_set_sda();
	MY_DELAY_US(1);
	i2c_set_scl();
	MY_DELAY_US(1); /*tHD;DAT*/		
	ack = (i2c_get_sda()? 0:1);	
	MY_DELAY_US(1);  /*nHigh*/
	i2c_clear_scl();
	
	return (ack);
}

/**-----------------------------------------------------------------------
  * @brief  主机从芯片读取一个字节
  * @param  要发送给芯片的应答或非应答
  * @retval 读取到的一个字节数据
-------------------------------------------------------------------------*/
uint8_t I2C_master_read(uint8_t ack)
{ 
	uint8_t mask = 0x80, b = 0;
	i2c_set_sda();
	do
	{	
		MY_DELAY_US(1);	
		i2c_set_scl();
		MY_DELAY_US(1);
		sda_rd_control(b);
		i2c_clear_scl();
		MY_DELAY_US(1);
	}while ((mask>>=1) != 0);	
	/* ACK slot control */
	if(ack != 0)
	{
		i2c_clear_sda();		/* SDA=0 for ACK*/
	}
	else
	{
		i2c_set_sda();		/* SDA=1 for NACK*/
	}	
	MY_DELAY_US(1);
	i2c_set_scl();
	MY_DELAY_US(1);
	i2c_clear_scl();
	MY_DELAY_US(1);
	return (b);
}
